﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web;
using System.IO;
using System.Text.RegularExpressions;
using System.Reflection;

namespace WxPayAPI
{
    public class PageJsApiPay : Page
    {
        public static string wxJsApiParam { get; set; } //H5调起JS API参数
        protected string orderCode = "";
        protected string PayResultPageUrl = "";
        protected string discription = "";
        protected string total_fee = "";
        protected string orderQueryPageUrl= "";

        protected string msg = "";

        protected override void OnLoad(EventArgs e)
        {
            orderCode = Request.QueryString["orderCode"];
            orderQueryPageUrl = Request.QueryString["orderQueryPageUrl"];
            Log.Info(this.GetType().ToString(), "orderCode " + orderCode + " JsApiPayPage.aspx page load");
            if (!IsPostBack)
            {
                //若传递了相关参数，则调统一下单接口，获得后续相关接口的入口参数
                JsApiPay jsApiPay = new JsApiPay(this);
                //string openid = Request.QueryString["openid"];
                total_fee = Request.QueryString["total_fee"];
                //检测是否给当前页面传递了相关参数
                if (string.IsNullOrEmpty(jsApiPay.openid) || string.IsNullOrEmpty(total_fee))
                {
                    msg = "服务器繁忙，请返回重试";
                    Log.Error(this.GetType().ToString(), "orderCode " + orderCode + "This page have not get params, cannot be inited, exit...");
                }
                else
                {
                   
                    //jsApiPay.openid = openid;

                    //JSAPI支付预处理
                    try
                    {
                        jsApiPay.total_fee = Convert.ToInt32(Convert.ToDecimal(total_fee) * 100);
                        //out_trade_no = orderCode;
                        PayResultPageUrl = HttpUtility.UrlDecode(Request.QueryString["PayResultPageUrl"]);
                        string bodyStr = HttpUtility.UrlDecode(Request.QueryString["body"]);
                        WxPayData unifiedOrderResult = jsApiPay.GetUnifiedOrderResult(orderCode, bodyStr);//生成预付单
                        wxJsApiParam = jsApiPay.GetJsApiParameters();//获取H5调起JS API参数                    
                        Log.Debug(this.GetType().ToString(), "orderCode " + orderCode + "wxJsApiParam : " + wxJsApiParam);
                        //在页面上显示订单信息 
                        ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", GetJsApiCall());
                    }
                    catch (Exception ex)
                    {
                        msg = "服务器繁忙，请返回重试";


                    }
                }
                if (!string.IsNullOrEmpty(msg))
                {
                    msg = @"<div class=""weui_msg"">
                                     <div class=""weui_opr_area"">
                                       <span class=""weui_icon_warn""></span>
                                          " + msg + @"
                                       </div>
                                   </div>";

                    Form.InnerHtml = msg;
                }
            }


        }

        public string GetJsApiCall()
        {
            string htmlStr =  WxPayAPI.Properties.Resources.JsApiPayPage;
            Match matchJs = Regex.Match(htmlStr, "<script\\s*type=\"text/javascript\"\\s*id=\"JsApiPayPage\"\\s*>[\\s\\S]*?</script>");
            string str = matchJs.Value;
            //string[] nameArray = new string[] { "wxJsApiParam", "out_trade_no", "sucessurl" };
            str = Regex.Replace(str, "<%[\\s]*=[\\s]*" + "wxJsApiParam" + "[\\s]*%>", wxJsApiParam);
            str = Regex.Replace(str, "<%[\\s]*=[\\s]*" + "orderCode" + "[\\s]*%>", orderCode);
            str = Regex.Replace(str, "<%[\\s]*=[\\s]*" + "PayResultPageUrl" + "[\\s]*%>", PayResultPageUrl);
            str = Regex.Replace(str, "<%[\\s]*=[\\s]*" + "orderQueryPageUrl" + "[\\s]*%>", orderQueryPageUrl);
            return str;
        }
    }
}
